<template>
	<view class="container">
		<view class="user-section">
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="userInfo.AvatarUrl || '/static/missing-face.png'"></image>
				</view>
				<view class="info-box">
					<view class="username">{{ member[0].NiName }}</view>
					<view class="username">{{ member[0].Number }}</view>
				</view>
				<view v-if="id==0" class="navigator" @click="navTo('/pages/public/login')">去登陆></view>
			</view>
		</view>

		<view 
			class="cover-container" 
			:style="[{ transform: coverTransform, transition: coverTransition }]" 
			@touchstart="coverTouchstart" 
			@touchmove="coverTouchmove" 
			@touchend="coverTouchend">
			<view class="member" :class="{'none': id==0}" :style="{'backgroundImage': 'url('+ BJimg +')'}">
				<text class="member-card">
					<text class="iconfont icon-dianpu card-icon"></text> 
					<text>{{member[0].CorpName}}</text> 
				</text>
				<text class="member-card">
					<text class="iconfont icon-huiyuan card-icon"></text>
					<text>{{member[0].ClassName}}</text>
				</text>
				<!-- 0折扣卡 1会员价 2零售价 FTypeID -->
				<text class="member-card" v-if="member[0].FTypeID==0">
					<text>折扣:{{member[0].FDiscount}}</text>
					<text></text>
					<text v-if="member[0].FDiscount === 1">(说明:目前会员等级低,暂时没有折扣)</text>
					<text v-else>(说明:目前会员可以打{{member[0].FDiscount}}折)</text>
				</text>
				<text class="member-card" v-else-if="member[0].FTypeID==2">
					<text>折扣:{{member[0].FDiscount}}</text>
					<text></text>
					<text>(说明:目前会员卡,暂时没有折扣)</text>
				</text>
				<text class="member-card" v-else="">
					<!-- <text>会员价:{{member[0].FDiscount}}</text>
					<text></text> -->
					<text>(说明:目前会员可以使用会员价)</text>
				</text>
				<!-- <text class="member-card">
					当前成长值:()
				</text> -->
				<view class="canvas">
					<canvas canvas-id="barcode"></canvas>
				</view>
				<text class="member-card">
					电话号码:<text>{{member[0].FPhone}}</text>
				</text>
			</view>
			
			<view class="tj-sction">
				<view class="tj-item" @click="navTo('/pages/record/balance')">
					<text class="num">{{member[0].Amount||0}}</text>
					<text>余额</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/record/record?id=1')">
					<text class="num">{{member[0].Ponit||0}}</text>
					<text>积分</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/coupon/coupon')">
					<text class="num">{{couponNumber}}</text>
					<text>优惠券</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/card/card')">
					<text class="num">{{cardNumber}}</text>
					<text>次卡</text>
				</view>
			</view>
	
			<view class="opt-section u-flex u-flex-wrap">
				<view class="order-item" @click="$u.route('/pages/MemberTopUp/MemberTopUp')">
					<u-icon name="vipcard" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">会员充值</view>
				</view>
				<view class="order-item" @click="$u.route('/pages/coupon/timeCard')">
					<u-icon name="order_pay" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">次卡购买</view>
				</view>
				<view class="order-item" @click="$u.route('/pages/card/card/')">
					<u-icon name="changciweixuanzhongGAI" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">次卡结存</view>
				</view>
				<view class="order-item" @click="$u.route('pages/coupon/coupon')">
					<u-icon name="ticket_coupon" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">优惠券</view>
				</view>
				<view class="order-item" @click="$u.route('/pages/record/balance')">
					<u-icon name="redpacket" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">充值明细</view>
				</view>
				<view class="order-item" @click="$u.route('/pages/record/record?id=1')">
					<u-icon name="ticket_integral" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">积分明细</view>
				</view>
				<view class="order-item" @click="$u.route('/pages/userinfoSet/userinfoSet')">
					<u-icon name="edit" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">完善资料</view>
				</view>
				<view class="order-item" @click="toShop">
					<u-icon name="shop" custom-prefix="custom-icon" size="50" color="#DD524D" />
					<view class="u-m-t-10">积分商城</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import listCell from '@/components/mix-list-cell';
	import { mapState, mapMutations } from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			listCell
		},
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				member: '', //会员信息
				// couponNumber: '',//优惠劵数量
				id: 0,
				BJimg: '',
				scrollHeight: 0,
				timer: null,
			}
		},
		onLoad(options){
			let id = uni.getStorageSync('LoginID') || 0; //登录状态码
			if(options.scene){
				id = 0
				uni.setStorageSync('scene', options.scene);
			}
			if(id == 0){
				this.MemberLogin()
			}
			let member = uni.getStorageSync('Members');//会员信息
			let token = uni.getStorageSync('Token'); //Token值
			this.$Url = uni.getStorageSync('URL') || this.$util.getUrls();
			this.id = id;
			let phone = uni.getStorageSync('phone')
			uni.showLoading({
				title: '加载中'
			})
			this.GetUserInfo();
		},
		onShow() {
			let id = uni.getStorageSync('LoginID') || 0 //登录状态码
			let that = this
			if(id == 1){
				that.GetUserInfo();
			}
		},
		onHide(){
			clearTimeout(this.timer)
			this.timer = null
		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['couponNumber','cardNumber','userInfo'])
		},
		onPullDownRefresh(){
			this.GetUserInfo();
		},
		methods: {
			...mapMutations(['onCoupon', 'onCard', 'login']),
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			//跳转到另一个小程序
			toShop(){
				uni.navigateToMiniProgram({
					appId: 'wxea7d284ff9b8d244',
					path: 'package/integral/index',
					extraData: {
						'business_key': uni.getStorageSync('business_key')
					},
					// 这个不写的话会显示开发环境,不能正常跳转,写上就能正常跳转了
					envVersion: 'release',
					success(res){
						console.log('goto', res)
					}
				})
			},
			navTo(url) {
				let id = uni.getStorageSync('LoginID');
				if (id != 1) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},
			miniProgram() {
				/**
				 * 注意: 这里做的是线上版本的，如果要测试本地版本，
				 * 一定要去会员卡小程序哪里把地址改为本地地址才可以
				 * 一定要注意这一点
				 * 
				 * 商城小程序这里不用，根据开发者前面设置的地址来测试，
				 * 线上就线上，本地就本地，只是上面这一点要记得
				 */
				let stockId = uni.getStorageSync('stockId');
				let hostid = uni.getStorageSync('hostid')
				let token = uni.getStorageSync('Token');
				let scan = {
					FItemID: stockId,
					parentID: '',
					hostid: hostid,
					PagePath: 'Member',
				}
				let data = {
						scan: scan,
					},
					url = 'pages/index/index';
				// this.$api.msg('功能暂未开通');
				this.$util.miniProgram(url, data);
			},

			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}
				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			},
			//获取用户会员卡的优惠劵
			MemberUser() {
				let number = 0;
				this.$util.util('MemberShop/CouponList', {}, 'POST').then(res => {
					res.forEach(item => {
						let timestamp = Date.parse(new Date());
						if (item.FStatus == 0) {
							let tiem = parseInt(item.FEndTime.slice(6, item.FEndTime.length - 2));
							if (timestamp < tiem) {
								number++;
							}
						}
					})
					this.onCoupon(number);
					this.MemberCard();
					uni.hideLoading();
				})
			},
			//次卡信息
			MemberCard() {
				this.$util.util('membershop/MemberTimeGroup', '', 'POST').then(res => {
					if (res != null)
						this.onCard(res.FQty);
				}).catch(error => console.log(error));
			},
			//会员卡信息
			MemberData() {
				this.$util.util('MemberShop/GetMemberData', {mSystemAppID:uni.getStorageSync('mSystemAppID')}, 'GET').then(res => {
					this.member = res;
					uni.setStorageSync('Members', res);
					uni.hideLoading();
					this.MemberBJ();
				})
			},
			//会员卡背景图
			MemberBJ() {
				let data = {
					corpid: this.$corpid, //公司id
					hostid: this.$hostid,
				}
				this.$util.util('WeChatShop/SystemParam', data, 'POST').then(res => {
					this.BJimg = res.FMiniBGK;
					let MemberCard = uni.getStorageSync('Members');
					this.$canvas.barcode('barcode', MemberCard[0].Number, 480, 380);
					this.MemberUser();
				}).catch(error => console.log(error))
			},
			// 自动登录 
			MemberLogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						let code = res.code;
						//抛异常
						try {
							this.$util.Login(code).then(res => {
								this.PhoneID = uni.getStorageSync('PhoneID');
								this.userInfo = uni.getStorageSync('userInfo');
								this.id = 1;
								//获取用户会员卡信息
								this.MemberData();
								if (res == 1 && this.PhoneID == 1) {
									uni.hideLoading();
								} else {
									uni.hideLoading();
								}
							}).catch(err=>{
								console.log('errrrr000', err)
							})
						} catch (e) {
							console.log('catch', e);
						}
					}
				})
			},
			GetUserInfo(data) {
				this.$util.get('MemberShop/GetMemberData', {mSystemAppID:uni.getStorageSync('mSystemAppID')}).then(res => {
					this.member = res.data;
					uni.stopPullDownRefresh
					uni.setStorageSync('Members', res.data);
					let uifo = {
						nickName: res.data[0].NiName,
						phone: res.data[0].Number
					}
					this.login(uifo)
					uni.hideLoading();
					this.MemberBJ();
				})
			},
		}
	}
</script>
<style lang='scss'>
	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	page {
		background-color: #f5f5f5;
	}

	.user-section {
		height: 340upx;
		padding: 0 30upx;
		position: relative;
		overflow: hidden;
		&::after{
			content: '';
			width: 120%;
			height: 120px;
			position: absolute;
			left: -10%;
			top: 0;
			z-index: -1;
			border-radius: 0 0 40% 40%;
			background: #DD524D;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			font-size: $font-lg + 6upx;
			color:#fefefe;
			margin-left: 20upx;
		}
	}

	.vip-card-box {
		display: flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0, 0, 0, .7), rgba(0, 0, 0, .8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;

		.card-bg {
			position: absolute;
			top: 20upx;
			right: 0;
			width: 380upx;
			height: 260upx;
		}

		.b-btn {
			position: absolute;
			right: 20upx;
			top: 16upx;
			width: 132upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 22upx;
			color: #36343c;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
		}

		.tit {
			font-size: $font-base+2upx;
			color: #f7d680;
			margin-bottom: 28upx;

			.yticon {
				color: #f6e5a3;
				margin-right: 16upx;
			}
		}

		.e-b {
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}

	.cover-container {
		margin-top: -170upx;
		padding: 0 30upx;
		position: relative;
		padding-bottom: 20upx;
	}

	.tj-sction {
		@extend %section;

		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			color: #75787d;
		}

		.num {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}
	.opt-section {
		background-color: #fff;
		border-radius: 20upx;
		padding: 28upx 0;
		margin-top: 20upx;
		.order-item {
			@extend %flex-center;
			width: 25%;
			height: 120upx;
			border-radius: 10upx;
			color: $font-color-dark;
		}
		.yticon {
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #DD524D;
		}
		.icon-shouhoutuikuan {
			font-size: 44upx;
		}
	}
	.order-section {
		@extend %section;
		padding: 28upx 0;
		margin-top: 20upx;
		.order-item {
			@extend %flex-center;
			width: 25%;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;
		}
		.yticon {
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #DD524D;
		}
		.icon-shouhoutuikuan {
			font-size: 44upx;
		}
	}

	.history-section {
		/* padding: 30upx 0 0; */
		margin-top: 20upx;
		background: #fff;
		border-radius: 10upx;

		.sec-header {
			display: flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;

			.yticon {
				font-size: 44upx;
				color: #5eba8f;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}

		.h-list {
			white-space: nowrap;
			padding: 30upx 30upx 0;

			image {
				display: inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}
	}

	.history-list {
		display: flex;
		-webkit-box-align: center;
		-ms-flex-align: center;
		align-items: center;
		-ms-flex-wrap: wrap;
		flex-wrap: wrap;

		list-cell {
			width: 25%;
		}
	}

	.navigator {
		font-size: 38upx;
		color: #fff;
		margin-left: 100upx;
	}

	.member {
		padding: 0 30upx;
		height: 390upx;
		border-radius: 30upx;
		margin-bottom: 20upx;
		display: flex;
		flex-wrap: wrap;

		.member-card {
			width: 100%;
			height: 42upx;
			line-height: 42upx;
			display: inline-block;

			.card-icon {
				margin-right: 14upx;
			}
		}

		.member-card:first-child {
			margin-top: 34upx;
		}

		.canvas {
			width: 500upx;
			height: 104upx;
			background-color: #fff;
			margin: auto;
		}

		canvas {
			width: 500upx;
			height: 104upx;
		}
	}
</style>
